package com.scorpio.security.resolver;

import com.scorpio.agentuser.domain.AgentUserEntity;
import com.scorpio.common.constant.Constants;
import com.scorpio.security.annotation.PersonalLoginUser;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class PersonalLoginUserResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterType().isAssignableFrom(AgentUserEntity.class) && methodParameter.hasParameterAnnotation(PersonalLoginUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        Object info = nativeWebRequest.getAttribute(Constants.LOGIN_USER_KEY, RequestAttributes.SCOPE_REQUEST);
        return info;
    }
}
